#define _CRT_SECURE_NO_WARNINGS 1
//SeqList.c
#include"SeqList.h"

int lobby()
{
	int input = 0;

	printf("*************Sequential List*************\n");
	printf("*    1.PushBack          2.PushFront    *\n");
	printf("*    3.PopBack           4.PopFront     *\n");
	printf("*    5.Insert            6.Erase        *\n");
	printf("*    7.Print             8.Find         *\n");
	printf("*    9.Modify            10.Exit        *\n");
	printf("*****************************************\n");

	printf("Please input your choice:>");
	scanf("%d", &input);

	return input;
}

void SLInit(SL* psl)
{
	psl->a = (SLDatatype*)malloc(sizeof(SLDatatype) * 4);
	if (psl->a == NULL)
	{
		perror("malloc failed");
		return;
	}
	psl->size = 0;
	psl->capacity = 4;
}

void SLCheackCapacity(SL* psl)
{
	if (psl->size == psl->capacity)
	{
		SLDatatype* temp = (SLDatatype*)realloc(psl->a, sizeof(SLDatatype) * psl->capacity * 2);
		if (temp == NULL)
		{
			perror("realloc failed");
			return;
		}
		psl->a = temp;
		psl->capacity *= 2;
	}
}

void SLDestory(SL* psl)
{
	free(psl->a);
	psl->a = NULL;
}

void SLPrint(SL* psl)
{
	for (int i = 0; i < psl->size; i++)
	{
		printf("%d ", psl->a[i]);
	}
	printf("\n");
}


void SLInsert(SL* psl, int pos, SLDatatype x)
{
	assert(pos >= 0 && pos <= psl->size + 1);

	SLCheackCapacity(psl);

	for (int i = psl->size; i >= pos; i--)
	{
		psl->a[i] = psl->a[i - 1];
	}
	psl->a[pos - 1] = x;

	psl->size++;
}

void SLPushBack(SL* psl, SLDatatype x)
{
	SLCheackCapacity(psl);

	SLInsert(psl, psl->size + 1, x);
}

void SLPushFront(SL* psl, SLDatatype x)
{
	SLCheackCapacity(psl);

	SLInsert(psl, 1, x);
}

void SLErase(SL* psl, int pos)
{
	assert(pos >= 0 && pos < psl->size + 1);

	for (int i = pos; i < psl->size; i++)
	{
		psl->a[i - 1] = psl->a[i];
	}
	psl->size--;
}

void SLPopBack(SL* psl)
{
	assert(psl->size > 0);

	/*if (psl->size == 0)
	{
		return;
	}*/

	SLErase(psl, psl->size);
}

void SLPopFront(SL* psl)
{
	assert(psl->size > 0);

	SLErase(psl, 0);
}

void SLFind(SL* psl, SLDatatype x)
{
	for (int i = 0; i < psl->size; i++)
	{
		if (psl->a[i] == x)
		{
			printf("'%d' was founded at >%d<\n", x, i);
			return;
		}
	}
	printf("Not founded!");
}

void SLModify(SL* psl, int pos, SLDatatype x)
{
	assert(pos >= 0 && pos < psl->size);

	psl->a[pos - 1] = x;
}